package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.PersonCommentAddAdapter;
import com.example.safeassistan.home.model.InvestigationFinishMOdel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.ToastUtil;
import com.example.safeassistan.widget.CommentScoreBottomDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PersonCommentAddList extends BaseActivity implements View.OnClickListener {
    private PersonCommentAddAdapter adapter;
    private RecyclerView rcView;
    private TextView tvSubmit1;
    private LinearLayout llNodata;

    private InvestigationFinishMOdel mOdel;
    private String riskPointId="";//风险点id
    private List<Map> userList;


    public static void actionStart(Context context, InvestigationFinishMOdel mOdel,String riskPointId) {
        Intent intent = new Intent(context, PersonCommentAddList.class);
        intent.putExtra("mOdel",(Serializable) mOdel);
        intent.putExtra("riskPointId",riskPointId);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.person_comment_add;
    }

    @Override
    protected void initView() {
        setTitleText("人员评价");
        mOdel=(InvestigationFinishMOdel) getIntent().getSerializableExtra("mOdel");
        riskPointId=getIntent().getStringExtra("riskPointId");
        rcView=findViewById(R.id.rcView);
        llNodata=findViewById(R.id.llNodata);
        tvSubmit1=findViewById(R.id.tvSubmit1);
        tvSubmit1.setOnClickListener(this);

        if (mOdel.getResult().getUserList()!=null&&mOdel.getResult().getUserList().size()>0){
            llNodata.setVisibility(View.GONE);
            tvSubmit1.setVisibility(View.VISIBLE);
            userList=new ArrayList<>(mOdel.getResult().getUserList().size());
            for (int i=0;i<mOdel.getResult().getUserList().size();i++){
                Map map=new HashMap();
                map.put("bodyStatus",0);
                map.put("spiritStatus",0);
                map.put("userId",mOdel.getResult().getUserList().get(i).getUserId());
                userList.add(map);
            }

            adapter=new PersonCommentAddAdapter(mOdel.getResult().getUserList());
            rcView.setLayoutManager(new LinearLayoutManager(getContext()));
            rcView.setAdapter(adapter);
            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (view.getId()){
                        case R.id.llBody:
                            //身体状态
                            CommentScoreBottomDialog dialog=new CommentScoreBottomDialog();
                            dialog.setClicklistener(new CommentScoreBottomDialog.ClickListenerInterface() {
                                @Override
                                public void selectScore(int score) {
                                    userList.get(position).put("bodyStatus",score);
                                    mOdel.getResult().getUserList().get(position).setBodyStatus(score);
                                    adapter.notifyItemChanged(position,R.id.tvBodyScore);
                                    dialog.dismiss();
                                }
                            });
                            dialog.show(getSupportFragmentManager());
                            break;
                        case R.id.llEssence:
                            //精神状态
                            CommentScoreBottomDialog dialog1=new CommentScoreBottomDialog();
                            dialog1.setClicklistener(new CommentScoreBottomDialog.ClickListenerInterface() {
                                @Override
                                public void selectScore(int score) {
                                    userList.get(position).put("spiritStatus",score);
                                    mOdel.getResult().getUserList().get(position).setSpiritStatus(score);
                                    adapter.notifyItemChanged(position,R.id.tvEssenceScore);
                                    dialog1.dismiss();
                                }
                            });
                            dialog1.show(getSupportFragmentManager());
                            break;
                    }
                }
            });
        }else {
            llNodata.setVisibility(View.VISIBLE);
            tvSubmit1.setVisibility(View.GONE);
        }
    }

    public void submitData(){
        JSONObject map=new JSONObject();
        try {
            map.put("recordTableId",mOdel.getResult().getDefRecordTableId());//检查表id
            map.put("riskPointId",riskPointId);//风险点id
            JSONArray g = new JSONArray(userList);
            map.put("userList",g);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new HttpRequest(this).doPost(UrlConstants.checkpersonScore, null, map, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean  success = jsonObject.getBoolean("success");
                    if (success) {
                        ToastUtil toastUtil=new ToastUtil(PersonCommentAddList.this,R.layout.popu_success,"评分完成！！",1);
                        toastUtil.show();
                        finish();
                    }else {
                        Toast.makeText(PersonCommentAddList.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int status, String msg) { }
        });
    }



    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSubmit1:
                submitData();
                break;
        }
    }
}